<?php
class ValidateFunc {
	
	var $validateUtil;
	var $configure;
	var $validate_config;
	var $validate_msg_config;
	
	function ValidateFunc($configure) {
		$this->validateUtil = new ValidateUtil();
		$this->configure = $configure;
		$this->validate_config = $configure->get_validate_config();
		$this->validate_msg_config = $configure->get_validate_msg_config();
	}
	
	function cv_checkIsNull($id, $advice_id, $descName, $params, $error_list) {
	
		$value = $this->get_value($id);
		if(!isset($value) || $value == "") {
			
			$error_list[] = $this->add_msg(
								$advice_id, 
								$this->format_msg($this->get_msg('cv_checkIsNull'), array($descName)));
		}
		return $error_list;
	}
	
	function cv_checkLengthRange($id, $advice_id, $descName, $params, $error_list) {
		$value = $this->get_value($id);
		$min = $params[0];
		$max = $params[1];
	
		if(mb_strlen($value) > $max || mb_strlen($value) < $min) {
			$error_list[] = $this->add_msg(
								$advice_id, 
								$this->format_msg($this->get_msg('cv_checkLengthRange'), array($descName, $min, $max)));
		}
		return $error_list;
	}
	
	function cv_checkIsNumber($id, $advice_id, $descName, $params, $error_list) {
		
		$value = $this->get_value($id);
		if(!is_numeric($value)) {
			
			$error_list[] = $this->add_msg(
								$advice_id, 
								$this->format_msg($this->get_msg('cv_checkIsNumber'), array($descName)));
		}
		return $error_list;
	}
	
	function cv_checkIsInt($id, $advice_id, $descName, $params, $error_list) {
		
		$value = $this->get_value($id);
		if(!preg_match("/^\d+$/", $value)) {
			$error_list[] = $this->add_msg(
								$advice_id, 
								$this->format_msg($this->get_msg('cv_checkIsInt'), array($descName)));
		}
		return $error_list;
	}
	
	function get_value($id) {
		
		return $this->configure->get_value($id);
	}
	
	function add_msg($advice_id, $msg) {
		return $this->validateUtil->add_msg($advice_id, $msg);
	}
	
	function get_msg($msgId) {
		
		return $this->validate_msg_config[$this->validate_config['language']][$msgId];
	}
	
	function format_msg($msg, $params) {
		for($i = 0; $i < count($params); $i++) {
			$msg = str_replace('%'.($i+1).'%', $params[$i], $msg);
		}
		return $msg;
	}
}
?>
